package alexAI;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Utils class contains useful functions that don't necessarily fit within other classes currently exist.
 */
public class Utils
{
	
	/**
	 * Returns the current time stamp of the system. (possible alt: long time1 = System.currentTimeMillis();)
	 * @return the current time stamp
	 */
	public static String getTimeStamp() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");  
		df.setTimeZone(TimeZone.getTimeZone("PST"));  
		return df.format(new Date());  
	}

	/**
	 * Splits the move String into a 2D array that contains the x,y position of the current piece, and
	 * x,y position of the move to make.
	 * @param move String that contains the move to make
	 * @return a 2D array that contains the previous position and the next position
	 */
	public static int[][] getMove(String move) {
		move = move.trim();
		int[][] oppMove = new int[2][2];
		if(move.length() > 4) {
			String[] tmp = move.trim().split("-");
		
			 oppMove[0] = boardCoordToInt(tmp[0]);
			 oppMove[1] = boardCoordToInt(tmp[1]);
		} else {
			oppMove[0] = boardCoordToInt(move.charAt(0) + "" + move.charAt(1));
			oppMove[1] = boardCoordToInt(move.charAt(2) + "" + move.charAt(3));
		}
				
		return oppMove;
	}
	
	/**
	 * Converts a String of movements to a 2D array of movements
	 * @param bCoords the String that contains the movement to be made
	 * @return a 1D array containing the move to be played
	 */
	public static int[] boardCoordToInt(String bCoords) {
		int[] intCoords = new int[2];
		
		bCoords = bCoords.trim();	
		
		intCoords[0] = ((int)bCoords.charAt(0) - 65);
		intCoords[1] = Integer.parseInt((bCoords.charAt(1)+"")) - 1;
		
		return intCoords;
	}
	
	/**
	 * Convert the 2D array of (x,y) From and (x,y) To to a String that can be understood by the server
	 * @param move the 2D integer array.
	 * @return return a String formatted to be understood by the server
	 */
	public static String intToBoardCoord(int[][] move)
	{
		String strMove = "";
		strMove += (char)(move[0][0]+65);
		strMove += (move[0][1] + 1);
		strMove += (char)(move[1][0]+65);
		strMove += (move[1][1] + 1);
	
		return strMove;
	}
	
	public static String intToBoardCoordFormatted(int[][] move)
 {
  String strMove = "(";
  strMove += (char)(move[0][0]+65);
  strMove += (move[0][1] + 1);
  strMove += " --> ";
  strMove += (char)(move[1][0]+65);
  strMove += (move[1][1] + 1);
  strMove += ")";
  
  return strMove;
 }
	
	/**
	 * Changes the board integers into more readable characters for printing to the console.
	 *<br /> 0 = "-"
	 *<br /> 1 = "o"
	 *<br /> 2 = "O"
	 *<br /> 3 = "x"
	 *<br /> 4 = "X"
	 * @param piece the piece integer
	 * @return a String representing the piece
	 */
	public static String ConvertIntToGamePieces(int piece) {
		if(piece == 0) 
			return "-";
		if(piece == 1) 
			return "o";
		if(piece == 2) 
			return "O";
		if(piece == 3) 
			return "x";
		if(piece == 4)
			return "X";
		return "-1"; // If function returns -1 TWITCHY TAIL DANGER!!!!!
	}

	public static void printBoard(int[][] board) {
		 System.out.println(); //Make sure nothing gets in the way of the board
		 for(int i = 8-1; i >= 0; i--) {
			 System.out.print(i+1 + "| ");
			 for(int k = 0; k < 8; k++) {
				 System.out.print(Utils.ConvertIntToGamePieces(board[k][i]) + " ");
			 }
			 System.out.println();
			 if(i==0) {
				System.out.println("===================");
				System.out.println("   A B C D E F G H ");
			 }
		 }
	}
	
}
